Optional optimizer-extensions
{ ideas
   could add an extension to check the last up-to-4 instructions compiled
   and eliminate these:
      49E3B0   4 # EBP ADD                    83C504
      49E3B3   4 # EBP SUB                    83ED04


      49E3AD   0 [EBP] EBX MOV                8B5D00
      49E3B0   4 # EBP ADD                    83C504
      49E3B3   4 # EBP SUB                    83ED04
      49E3B6   EBX 0 [EBP] MOV                895D00

      473972   8 # EBP ADD                    83C508
      473975   4 # EBP SUB                    83ED04
         -> should just turn into 4 # EBP ADD
}

-? variable opts
package optimizing-compiler


: lit+@
   [+asm]
   lastlit @ [ebx] ebx mov
   [-asm]
   1 opts +! ;
optimize lit-plus @ with lit+@

: lit+!
   [+asm]
   0 [EBP] EAX MOV
   eax lastlit @ [ebx] mov
   4 [EBP] EBX MOV
   8 # EBP ADD
   [-asm]
   1 opts +! ;
optimize lit-plus ! with lit+!


: lit+h@
   [+asm]
      lastlit @ [ebx] ebx movsxw
   [-asm]
   1 opts +! ;
optimize lit-plus h@ with lit+h@

: lit+h!
   [+asm]
      0 [EBP] EAX MOV
      16bit: eax lastlit @ [ebx] mov
      4 [EBP] EBX MOV
      8 # EBP ADD
   [-asm]
   1 opts +! ;
optimize lit-plus h! with lit+h!


: lit-h@
   [+asm]
      lastlit @ [ebx] ebx movsxw
   [-asm]
   1 opts +! ;
optimize lit-plus h@ with lit-h@



: lit-!
   [+asm]
      lastlit @ # eax mov
      ebx 0 [eax] mov
      pop(ebx)
   [-asm]
   1 opts +! ;
optimize (literal) ! with lit-!

: lit-h!
   [+asm]
      lastlit @ # eax mov
      16bit: ebx 0 [eax] mov
      pop(ebx)
   [-asm]
   123 .
   1 opts +! ;
optimize (literal) h! with lit-h!


: swap>r
   [+asm]
    0 [ebp] push
    4 # ebp add
   [-asm]
  1 opts +! ;
optimize swap >r with swap>r

: @+swap
  [+asm]
    4 # ebp sub
    0 [ebx] eax mov
    eax 0 [ebp] mov
    4 # ebx add
  [-asm]  1 opts +! ;
optimize @+ swap with @+swap

: !-r@
  [+asm]
   0 [EBP] EAX MOV
   EAX 0 [EBX] MOV
   4 # EBP ADD
   0 [esp] ebx mov
  [-asm]  1 opts +! ;
optimize ! r@ with !-r@

end-package
gild
